package com.neuedu.LeetCode;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 和大于0尾指针向左移动。和小于0，头指针向右移动
 */
public class Test15 {
    public static void main(String[] args) {
        int[] num = {1, -1, 1, 0, 4, 2, -5, 5};

        List<List<Integer>> lists = threeSum(num);
    }

    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> total = new ArrayList<>();
        List<Integer> list = new ArrayList<>();

        //转换为集合，并排序
        for (int i = 0; i < nums.length; i++) {
            list.add(nums[i]);
        }
        List<Integer> newlist = list.stream().sorted((a, b) -> a.compareTo(b)).collect(Collectors.toList());

        //定义头尾指针
        int head = 0;
        int tail = newlist.size() - 1;



        for (int i = 0 ; i < newlist.size() ; i++){
            head = i+1;
            while ( head <= tail){
                if (newlist.get(i) + newlist.get(head) + newlist.get(tail) == 0) {
                    List value = new ArrayList();
                    value.add(newlist.get(i));
                    value.add(newlist.get(head));
                    value.add(newlist.get(tail));
                    total.add(value);
                    //条件

                }else if(newlist.get(i) + newlist.get(head) + newlist.get(tail) >=0){
                    tail--;

                }else {
                    head++;

                }
            }
            System.out.println("当前i："+i);
        }

        return total;
    }

}
